# pylint: disable-msg=W0232
"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey
from sqlalchemy.types import Integer, String, Numeric, Boolean, Text
from sqlalchemy.orm import relation, backref
from pylons.i18n import lazy_ugettext as _

from chowa.model.meta import Base
from chowa.model.geo import Locality

__all__ = ['Institution', 'Email',
           'District', 'Office',
           'DivisionType', 'Division', 'SuperDivision', 'SubDivision', 'Subordination']


class Institution(Base):
    __tablename__ = 'organization_institution'
    
    id = Column(Integer, primary_key=True)
    full_name = Column(String(255), info={'verbose': _('Full name')})
    short_name = Column(String(20), info={'verbose': _('Short name')})
    
    def __unicode__(self):
        return self.short_name


class Email(Base):
    __tablename__ = 'organization_email'
    
    id = Column(Integer, primary_key=True)
    address = Column(String(255), info={'verbose': _('Address')})
    description = Column(String(255), info={'verbose': _('Description')})
    institution_id = Column(Integer, ForeignKey(Institution.id), info={'verbose': _('Institution')})
    
    institution = relation(Institution)
    
    def __unicode__(self):
        return self.address


class District(Base):
    __tablename__ = 'organization_district'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), info={'verbose': _('Object|Name')})
    genitive = Column(String(100), info={'verbose': _('Genitive')})
    colloquial = Column(String(100), info={'verbose': _('Colloquial')})
    treasury_code = Column(Numeric(4), info={'verbose': _('Treasury code')})
    is_primary = Column(Boolean, default=False, info={'verbose': _('Is primary')})
    
    def __unicode__(self):
        return self.name

    
class Office(Base):
    __tablename__ = 'organization_office'
    
    id = Column(Integer, primary_key=True)
    verbose = Column(String(255))
    district_id = Column(Integer, ForeignKey(District.id), info={'verbose': _('District')})
    locality_id = Column(Integer, ForeignKey(Locality.id), info={'verbose': _('Locality')})
    street = Column(String(255), info={'verbose': _('Street')})
    building_number = Column(String(20), info={'verbose': _('Building number')})
    postal_code = Column(Integer, info={'verbose': _('Postal code')})
    
    district = relation(District, backref='offices')
    locality = relation(Locality)
    
    def __unicode__(self):
        return self.verbose


class DivisionType(Base):
    __tablename__ = 'organization_division_type'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(100), unique=True)
    plural = Column(String(100))
    description = Column(Text)
    min_size = Column(Integer)
    must_subordinate = Column(Boolean, default=False)
    
    def __unicode__(self):
        return self.name


class Division(Base):
    __tablename__ = 'organization_division'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(255), info={'verbose': _('Object|Name')})
    genitive = Column(String(255), info={'verbose': _('Genitive')})
    short_name = Column(String(255), info={'verbose': _('Short name')})
    code = Column(String(10), info={'verbose': _('Code')})
    district_id = Column(Integer, ForeignKey(District.id), info={'verbose': _('District')})
    type_id = Column(Integer, ForeignKey(DivisionType.id))
    is_superior = Column(Boolean)
    positions = Column(Integer)
    is_disabled = Column(Boolean, default=False, info={'verbose': _('Is disabled')})
    
    district = relation(District, backref='divisions')
    type = relation(DivisionType)
    
    __mapper_args__ = {'polymorphic_on': is_superior}
    
    def __unicode__(self):
        return '%s %s' % ('x' * self.is_disabled, self.name)

class SuperDivision(Division):
    __mapper_args__ = {'polymorphic_identity': True}
    
class SubDivision(Division):
    __mapper_args__ = {'polymorphic_identity': False}
    
class Subordination(Base):
    __tablename__ = 'organization_subordination'
    
    id = Column(Integer, primary_key=True)
    division_id = Column(Integer, ForeignKey(Division.id), unique=True, info={'verbose': _('Division')})
    superior_id = Column(Integer, ForeignKey(SuperDivision.id), info={'verbose': _('Superior')})
    
    division = relation(Division,
                        primaryjoin="Division.id == Subordination.division_id", 
                        order_by=Division.name, backref=backref('superior', uselist=False))
    superior = relation(SuperDivision,
                        primaryjoin="SuperDivision.id == Subordination.superior_id",
                        order_by=SuperDivision.name)
    
    def __unicode__(self):
        return '%s (%s)' % (unicode(self.division), unicode(self.superior))
